﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GRC_TPE_WebApp.Controller;

namespace GRC_TPE_WebApp.View.Private.Business
{
    public partial class EditBusiness : System.Web.UI.Page
    {
        int idBusiness = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            /// tentative de récupération de l'id
            if (!int.TryParse(Request.QueryString["id"], out idBusiness))
                /// redirection s'il y a une erreur au Parse
                Response.Redirect(Request.UrlReferrer.AbsolutePath);
        }

        /// <summary>
        /// Méthode déclenchée une fois la page chargée
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoadComplete(EventArgs e)
        {
            base.OnLoadComplete(e);

            if (this.idBusiness != 0)
            {
                string[] businessFields = BusinessController.GetBusinessFields(this.idBusiness);

                ///si tous les champs ont bien été retournés
                if (businessFields.Count() == 5)
                {
                    this.editBusinessTitle.Text = businessFields[0];
                    this.editBusinessDescription.Text = businessFields[2];
                    this.editBusinessClient.Text = businessFields[3];
                    this.editBusinessAmount.Text = businessFields[4];

                    /// gestion du statut
                    switch (businessFields[1])
                    {
                        case "Gagnée":
                            this.DDL_State.SelectedIndex = 0;
                            break;
                        case "Perdue":
                            this.DDL_State.SelectedIndex = 1;
                            break;
                        case "Prospect":
                            this.DDL_State.SelectedIndex = 2;
                            break;
                        default:
                            this.DDL_State.SelectedIndex = 3;
                            break;
                    }
                }
                else
                    /// redirection s'il y a une erreur au Parse
                    Response.Redirect(Request.UrlReferrer.AbsolutePath);
            }
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le bouton d'enregistrement
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void B_SaveBusiness_Click(object sender, EventArgs e)
        {
            decimal amount = 0;

            if(decimal.TryParse(this.editBusinessAmount.Text, out amount))
            {
                string ret = BusinessController.EditBusiness(this.idBusiness, this.editBusinessTitle.Text, this.DDL_State.SelectedValue, this.editBusinessDescription.Text, amount);
                if (ret == "No error")
                {
                    this.editBusinessErrorMessage.ForeColor = System.Drawing.Color.DarkGreen;
                    this.editBusinessErrorMessage.Text = "Affaire modifiée avec succès";
                }
                else
                {
                    this.editBusinessErrorMessage.ForeColor = System.Drawing.Color.DarkRed;
                    this.editBusinessErrorMessage.Text = ret;
                }
            }
            else
            {
                this.editBusinessErrorMessage.ForeColor = System.Drawing.Color.DarkRed;
                this.editBusinessErrorMessage.Text = "Le montant doit être un nombre décimal.";
            }
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le bouton d'annulation
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void B_Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("ListBusinesses.aspx");
        }
    }
}